﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Windows.Forms;
using BigTestV3.xmlHelp;
using RemotingSocket;

namespace BigTestV3.common
{
    public class NetFileTransferServer:Component
    {
        #region 变量
        private static List<string> listmsg = new List<string>();

        private TcpChannel _channel = null;

        private int _port = 8050;

        private string _registermethod = "FileService";

        private bool _active = false; 
        #endregion

        #region 属性
        public int Port
        {
            get { return this._port; }
            set { this._port = value; }
        }

        public string Registermethod
        {
            get { return this._registermethod; }
            set { this._registermethod = value; }
        }

        public bool Active
        {
            get { return this._active; }
        } 
        #endregion

        #region 方法
        public void Start()
        {
            try
            {
                _channel = new TcpChannel(this._port);
                ChannelServices.RegisterChannel(_channel,false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(NetFileTransfer), this._registermethod, WellKnownObjectMode.SingleCall);
                NetFileTransfer.SendMessageEvent += new SendMessageEventHandler(NetFileTransfer_SendMessageEvent);
                this._active = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                this._active = false;
            }
        }

        public void NetFileTransfer_SendMessageEvent(string msg)
        {
            listmsg.Add(msg);
            HashSet<string> hash = new HashSet<string>(listmsg);
            listmsg = hash.ToList();
           
            LoginSuccessStu stu = new LoginSuccessStu { StuNumber = listmsg };
            XmlHelper.XmlSerializeToFile(stu, Application.StartupPath + @"\xml\LoginSuccess.xml", Encoding.UTF8);
        }

        public void Stop()
        {
            this._active = false;
            if (_channel != null)
            {
                ChannelServices.UnregisterChannel(_channel);
            }
        }

        public string NoticeRegisterMethodName(int ipAddressIndex)
        {
            //tcp://localhost:8085/FileService
            try
            {
                IPHostEntry heserver = Dns.Resolve("localhost");
                IPAddress currAddr = heserver.AddressList[ipAddressIndex];
                return String.Format("tcp://{0}:{1}/{2}", currAddr.ToString(), this._port.ToString(), this._registermethod);
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }  
        #endregion
    }
}